package cn.wx.common.utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import org.springframework.stereotype.Component;

import java.security.KeyPair;

/**
 * 512位 RSA加密
 */
@Component
public class RsaUtils {
	private final static String publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKIK0WIuQQqbbE3+gE4wgNN6ASsonpSaw0+tEBalu0e3Y/3i8JO0a5hVdFkeoqRLZNrPfzN9cipY2V37uIIUbvUCAwEAAQ==";
	private final static String privateKey = "MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEAogrRYi5BCptsTf6ATjCA03oBKyielJrDT60QFqW7R7dj/eLwk7RrmFV0WR6ipEtk2s9/M31yKljZXfu4ghRu9QIDAQABAkBFCHbLbQkoi65IS/Q9W0yGy6NTz6o/hO9QMlp4S4LNuhS+aU5gbR3C9zLG0zudNOn6m/ysNze+VWh1VDv18XoBAiEA4w5DYbwuMeqGS4YZpQEyuAZIjAv58XVQ1+n0DQnabhkCIQC2sufEJzn0lIfO6Y6T293H1AhG2P2r7ycLejjN2horPQIgWFsxj3PqShRohDU//3zy3rfbPVxfvD2Z4xuwk+5UqOkCIAFxrI9KWBTsz539DplwlXIs5NvafzbKrx5SGvzVintpAiAMx9LBXqPs2F9yJgSSats48U/qfcHvAy6KsJVDNASzJw==";
	
	/**
	 * 私钥解密
	 *
	 * @param param
	 * @return
	 */
	public String decode(String param) {
//        RSA rsa = new RSA(privateKey,publicKey);
//        return rsa.decryptStr(param,KeyType.PrivateKey);
		return param;
	}
	
	
	/**
	 * 公钥加密
	 *
	 * @param param
	 * @return
	 */
	public String encode(String param) {
		RSA rsa = new RSA(privateKey, publicKey);
		return rsa.encryptStr(param, KeyType.PublicKey);
	}
	
	public static void main(String[] args) {
		KeyPair pair = SecureUtil.generateKeyPair("RSA", 512);
		System.out.println(Base64.encode(pair.getPrivate().getEncoded()));
		;
		System.out.println(Base64.encode(pair.getPublic().getEncoded()));
		;
		RSA rsa = new RSA(privateKey, publicKey);
		System.out.println(rsa.encryptStr("1", KeyType.PublicKey));
		;
		System.out.println(rsa.decryptStr("8DFCEEA86E7E96FE4882FFC62375F374F80803B95BC89F7DBEB88F7C61CEF1321FC2B1EB04E7F5E3FAA983522FE8922AABD121099F323B7F1260BCCC028D1705", KeyType.PrivateKey));
		;
		
	}
}
